package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.VideoComponent;
import com.go.where.family.constants.Constant;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.VideoInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.VideoService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.VideoVo;
import com.google.gson.Gson;

@Component("videoComponent")
public class VideoComponentImpl implements VideoComponent {

	private static final Logger log = Logger
			.getLogger(VideoComponentImpl.class);
	private Gson gson;
	@Resource(name = "videoService")
	private VideoService videoService;

	public VideoComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public List<VideoInfo> queryList(String body, Head head) {
		VideoVo vo = gson.fromJson(body, VideoVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("pageStart", pageStart);
			map.put("pageEnd", pageEnd);
			map.put("bigClassId", vo.getBigClassId());
			map.put("vodeoTime", vo.getVideoTime());
			map.put("isDelete", Constant.IS_NOT_DELETE);

			List<VideoInfo> list = videoService.queryList(map);
			if (list != null && !list.isEmpty()) {
				for (VideoInfo vi : list) {
					if (StringUtils.isNotBlank(vi.getVideoCover())) {
						vi.setVideoCover(CommonUtil.getUrlPrefix()
								+ vi.getVideoCover());
					}
				}
				return list;
			} else {
				return new ArrayList<VideoInfo>();
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取视频列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<VideoVo> queryHomeVideo(String body, Head head) {
		VideoVo vo = gson.fromJson(body, VideoVo.class);
		if (vo != null) {
			List<VideoInfo> list = videoService.queryGroupByBigClassAndTime();
			List<VideoVo> voList = new ArrayList<VideoVo>();
			if (list != null && !list.isEmpty()) {
				VideoVo vv = new VideoVo();
				for (VideoInfo info : list) {
					vv = new VideoVo();
					vv.setVideoId(info.getVideoId());
					vv.setBigClassId(info.getBigClassId());
					vv.setBigName(info.getBigName());
					vv.setVideoTime(info.getVideoTime());
					voList.add(vv);
				}
				return voList;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询首页视频列表信息时,没有查询到数据");
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取视频列表信息时，报文解析异常。");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public VideoInfo queryVideoDetail(String body, Head head) {
		VideoVo vo = gson.fromJson(body, VideoVo.class);
		if (vo != null) {
			String videoId = vo.getVideoId();
			VideoInfo vv = videoService.queryVideoDetail(videoId);
			if (vv != null) {
				if (StringUtils.isNotBlank(vv.getVideoCover())) {
					vv.setVideoCover(CommonUtil.getUrlPrefix()
							+ vv.getVideoCover());
				}
				return vv;
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",查询视频详情信息时,没有查询到数据,视频ID：" + videoId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",获取视频详情信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean update(String body, Head head) {
		VideoVo vo = gson.fromJson(body, VideoVo.class);
		if (vo != null) {
			String videoId = vo.getVideoId();
			VideoInfo vv = videoService.queryVideoDetail(videoId);
			if (vv != null) {
				Integer likeNum = vv.getVideoLike();
				if ("1".equals(vo.getLikeOrNot())) {
					likeNum = CommonUtil.add(likeNum, 1);
				} else if ("0".equals(vo.getLikeOrNot())) {
					likeNum = CommonUtil.sub(likeNum, 1);
				}
				VideoInfo vi = new VideoInfo();
				vi.setVideoId(videoId);
				vi.setVideoLike(likeNum);
				int re = videoService.update(vi);
				if (re > 0) {
					return true;
				} else {
					return false;
				}
			} else {
				log.info("requestId:" + head.getRequestId()
						+ ",更新视频点赞量或浏览量信息时,没有查询到数据,视频ID：" + videoId);
				throw new ServerDisponseException(TransmissionCode.NO_DATA
						.getCode(), TransmissionCode.NO_DATA.getMessage());
			}
		} else {
			log.info("requestId:" + head.getRequestId()
					+ ",更新视频点赞量或浏览量信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
